import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

// 导入持久化存储的第三方包
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  // 配置插件
  plugins: [createPersistedState()],

  state: {
    // token
    token: '',
    // 用户信息对象
    userInfo: {}
  },
  mutations: {
    setToken (state, newToken) {
      state.token = newToken
    },
    setUserInfo (state, info) {
      state.userInfo = info
    }
  },
  actions: {
    // axios获取用户信息
    async initUserInfo (context) {
      const { data: res } = await axios.get('/my/userinfo')

      if (res.code === 0) {
        context.commit('setUserInfo', res.data)
      }
    }
  },
  modules: {
  }
})
